package com.example.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.domain.entity.RestBean;
import com.example.backend.domain.po.LeaveRequests;
import com.example.backend.service.ILeaveRequestsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dyy
 * @since 2025-05-27
 */
@RestController
@RequestMapping("/leave-requests")
public class LeaveRequestsController {
    @Autowired
    private ILeaveRequestsService leaveRequestsService;

    // 根据 ID 查询单个请假记录
    @GetMapping("/{id}")
    public RestBean<LeaveRequests> getLeaveRequestById(@PathVariable Integer id) {
        LeaveRequests leaveRequest = leaveRequestsService.getById(id);
        return RestBean.success(leaveRequest);
    }

    // 根据多个 ID 查询请假记录
    @GetMapping("/batch")
    public RestBean<List<LeaveRequests>> getLeaveRequestsByIds(@RequestParam List<Integer> ids) {
        List<LeaveRequests> leaveRequests = leaveRequestsService.listByIds(ids);
        return RestBean.success(leaveRequests);
    }

    // 根据条件查询请假记录
    @GetMapping("/search")
    public RestBean<List<LeaveRequests>> searchLeaveRequests(
            @RequestParam(required = false) Integer employeeId,
            @RequestParam(required = false) String status) {
        LambdaQueryWrapper<LeaveRequests> queryWrapper = new LambdaQueryWrapper<>();
        if (employeeId != null) {
            queryWrapper.eq(LeaveRequests::getEmployeeId, employeeId);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq(LeaveRequests::getStatus, status);
        }
        List<LeaveRequests> leaveRequests = leaveRequestsService.list(queryWrapper);
        return RestBean.success(leaveRequests);
    }

    // 分页查询请假记录
    @GetMapping("/page")
    public RestBean<IPage<LeaveRequests>> getLeaveRequestsByPage(
            @RequestParam Integer current,
            @RequestParam Integer size,
            @RequestParam(required = false) Integer employeeId,
            @RequestParam(required = false) String status) {
        Page<LeaveRequests> page = new Page<>(current, size);
        LambdaQueryWrapper<LeaveRequests> queryWrapper = new LambdaQueryWrapper<>();
        if (employeeId != null) {
            queryWrapper.eq(LeaveRequests::getEmployeeId, employeeId);
        }
        if (status != null && !status.isEmpty()) {
            queryWrapper.eq(LeaveRequests::getStatus, status);
        }
        IPage<LeaveRequests> leaveRequestPage = leaveRequestsService.page(page, queryWrapper);
        return RestBean.success(leaveRequestPage);
    }

    // 新增请假记录
    @PostMapping
    public RestBean<Void> addLeaveRequest(@RequestBody LeaveRequests leaveRequest) {
        boolean save = leaveRequestsService.save(leaveRequest);
        return RestBean.success();
    }

    // 更新请假记录信息
    @PutMapping("/{id}")
    public RestBean<Void> updateLeaveRequest(
            @PathVariable Integer id,
            @RequestBody LeaveRequests leaveRequest) {
        leaveRequest.setId(id);
        boolean update = leaveRequestsService.updateById(leaveRequest);
        return RestBean.success();
    }

    // 删除请假记录
    @DeleteMapping("/{id}")
    public RestBean<Void> deleteLeaveRequest(@PathVariable Integer id) {
        boolean remove = leaveRequestsService.removeById(id);
        return RestBean.success();
    }
}
